<template>
    <!-- 成本还原总结模板 -->
    <base-breadcrumb>
        <div class="page-body">
            <a-card :bordered="false">
                <title-name title=" 成本还原" />
                <div class="table-operateors mb20" v-if="hideBuquBtn()">
                    <a-button type="primary" @click="handleUpdate('add', {}, typeId1)">新增行</a-button>
                </div>
                <list-table
                    ref="table1"
                    size="default"
                    rowKey="id"
                    :columns="columns"
                    :request="loadData1"
                    :alert="true"
                    show-size-changer
                    show-quick-jumper
                    showPagination="auto"
                >
                    <span slot="code" slot-scope="text, record">{{ record.code }}</span>

                    <template slot="operator" slot-scope="text, record">
                        <MoreButton>
                        <a
                            v-if="record.parentId == 'null'&&hideBuquBtn()"
                            class="base-btn"
                            @click="handleUpdate('addchild', record, typeId1)"
                            >添加子项</a
                        >
                        <a class="base-btn" @click="handleUpdate('edit', record, typeId1)" v-if="hideBuquBtn()">编辑</a>
                        <a class="base-btn" @click="handleUpdate('del', record, typeId1)" v-if="hideBuquBtn()">删除</a>
                        </MoreButton>
                    </template>
                </list-table>
            </a-card>
            <a-card :bordered="false">
                <title-name title=" 指标分析" />
                <div class="table-operateors mb20" v-if="hideBuquBtn()">
                    <a-button type="primary" @click="handleUpdate('add', {}, typeId2)">新增行</a-button>
                </div>
                <list-table
                    ref="table2"
                    size="default"
                    rowKey="id"
                    :columns="columns"
                    :request="loadData2"
                    :alert="true"
                    show-size-changer
                    show-quick-jumper
                    showPagination="auto"
                >
                    <span slot="code" slot-scope="text, record, index">{{ index + 1 }}</span>
                    <template slot="operator" slot-scope="text, record" v-if="hideBuquBtn()">
                        <a class="base-btn" @click="handleUpdate('edit', record, typeId2)">编辑</a>
                        <a-divider type="vertical" />
                        <a class="base-btn" @click="handleUpdate('del', record, typeId2)">删除</a>
                    </template>
                </list-table>
            </a-card>
            <a-card :bordered="false">
                <title-name title=" 施工做法" />
                <div class="table-operateors mb20" v-if="hideBuquBtn()">
                    <a-button type="primary" @click="handleUpdate('add', {}, typeId3)">新增行</a-button>
                </div>
                <list-table
                    ref="table3"
                    size="default"
                    rowKey="id"
                    :columns="columns"
                    :request="loadData3"
                    :alert="true"
                    show-size-changer
                    show-quick-jumper
                    showPagination="auto"
                >
                    <span slot="code" slot-scope="text, record, index">{{ index + 1 }}</span>

                    <template slot="operator" slot-scope="text, record" v-if="hideBuquBtn()">
                        <a class="base-btn" @click="handleUpdate('edit', record, typeId3)">编辑</a>
                        <a-divider type="vertical" />
                        <a class="base-btn" @click="handleUpdate('del', record, typeId3)">删除</a>
                    </template>
                </list-table>
            </a-card>
        </div>
        <update-template
            v-if="visible.update"
            :init-data="childInitData"
            @cancel="visible.update = false"
            @confirm="handleUpdateConfirm"
        />
    </base-breadcrumb>
</template>

<script>
import StatusDisplay from '@/components/StatusDisplay'
import {
    getSelectListByType,
    delSelectListByType,
    getSelectListTreeByType,
    delSelectListTreeByType,
} from '@/api/businessAffairs/common.js'
import updateTemplate from './updateTemplate'
import { STable } from '@/components'
export default {
    name:'a'+Date.now(),
    components: {
        STable,
        updateTemplate,
        StatusDisplay,
    },
    data() {
        return {
            childInitData: {},
            visible: {
                update: false,
            },
            columns: [
                {
                    title: '序号',
                    dataIndex: 'code',
                    width: 65,
                    scopedSlots: { customRender: 'code' },
                },
                {
                    title: '名称',
                    width: 100,
                    dataIndex: 'name',
                },

                {
                    title: ' 操作',
                    dataIndex: 'operator',
                    width: 180,
                    scopedSlots: { customRender: 'operator' },
                },
            ],
            typeId1: 12,
            typeId2: 13,
            typeId3: 11,
            loadData1: async (parameter) => {
                let requestParameters = Object.assign({}, parameter, {
                    type: this.typeId1,
                    isParent: true,
                })
                const [err, res] = await getSelectListTreeByType(requestParameters)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                        // console.log(res)
                        res.data.forEach((item) => {
                            item.children.forEach((i) => {
                                delete i.children
                            })
                            if (item.children.length == 0) {
                                delete item.children
                            }
                        })
                        console.log(res)
                        resolve(res)
                    }
                })
            },
            loadData2: async (parameter) => {
                let requestParameters = Object.assign({}, parameter, {
                    type: this.typeId2,
                })
                const [err, res] = await getSelectListByType(requestParameters)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(res)
                    }
                })
            },

            loadData3: async (parameter) => {
                let requestParameters = Object.assign({}, parameter, {
                    type: this.typeId3,
                })
                const [err, res] = await getSelectListByType(requestParameters)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(res)
                    }
                })
            },
        }
    },
    methods: {
        handleUpdateConfirm(typeId) {
            this.visible.update = false
            this.isRefresh(typeId)
        },
        isRefresh(typeId) {
            if (typeId == this.typeId1) {
                this.$refs.table1.refresh(true)
            } else if (typeId == this.typeId2) {
                this.$refs.table2.refresh(true)
            } else {
                this.$refs.table3.refresh(true)
            }
        },
        async delete(params,typeId) {
            if (typeId == 12) {
                const [err, res] = await delSelectListTreeByType(params)
                if (!err) {
                    this.$message.success('删除成功')
                    this.isRefresh(typeId)
                }
            } else {
                const [err, res] = await delSelectListByType(params)
                if (!err) {
                    this.$message.success('删除成功')
                    this.isRefresh(typeId)
                }
            }
        },
        // 处理更新操作（新增、编辑）
        async handleUpdate(type, data = {}, typeId) {
            if (type == 'del') {
                let params = {
                    id: data.id,
                }
                let that = this
                this.$confirm({
                    title:"温馨提示",
                content: '确定是否删除数据？',
                okText: '确认',
                okType: 'danger',
                cancelText: '取消',
                    onOk: () => {
                        return that.delete(params, typeId)
                    },
                })

                return
            } else {
                this.childInitData = {
                    ...data,
                    pageType: type,
                    typeId: typeId,
                }
                this.visible.update = true
            }
        },
    },
}
</script>

<style lang="less" scoped>
.page-header {
    .header-row {
        display: flex;
        .flex-content {
            flex: auto;
        }
        .divider {
            margin: 0 12px;
        }
        .expand-btn {
            margin-left: 8px;
            font-size: 14px;
        }
    }
}
.page-body {
    .table-operateors {
        .ant-btn + .ant-btn {
            margin-left: 12px;
        }
    }
}
</style>